#!/bin/sh

# Make sure only root can run our script
if [[ $EUID -ne 0 ]]; then
  echo "This script must be run as root" 1>&2
  exit 1
fi

case $1 in
start)
 #sysctl -w net.ipv4.conf.all.forwarding=1 && sysctl -w net.ipv6.conf.all.forwarding=1 && iptables -t nat -A POSTROUTING -s 192.169.133.0/24 -j MASQUERADE
  sysctl -w net.ipv4.conf.all.forwarding=1 && sysctl -w net.ipv6.conf.all.forwarding=1 && iptables -t nat -A POSTROUTING  -j MASQUERADE
 case $2 in
  http)
   iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 8123
   iptables -t nat -A PREROUTING -p tcp --dport 8081 -j REDIRECT --to 8123
   iptables -t nat -A PREROUTING -p tcp --dport 8080 -j REDIRECT --to 8123
   iptables -t nat -A PREROUTING -p tcp --dport 10000 -j REDIRECT --to 8123
   iptables -t nat -A PREROUTING -p tcp --dport 443 -j REDIRECT --to 8124
   iptables -t nat -A PREROUTING -p tcp --dport 8443 -j REDIRECT --to 8124
   iptables -t nat -A PREROUTING -p tcp --dport 5228 -j REDIRECT --to 8124
   systemctl start redsocks2
   ;;
  socks)
   iptables -t nat -A PREROUTING -p tcp -j REDIRECT --to 8123
   systemctl start redsocks2
 esac
 ;;
stop)
 iptables -t nat -F PREROUTING
 iptables -t nat -F POSTROUTING
 systemctl stop redsocks2
 ;;
*)
 echo "start or stop"
esac
